package servlet;

import servlet.User;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;

@WebServlet("/editUser")
public class EditUserServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String id = request.getParameter("id"); // 获取要编辑的用户 ID
        if (id == null || id.isEmpty()) {
            response.sendRedirect("userList"); // 如果 ID 为空，跳转到用户列表页面
            return;
        }

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/FinalTest", "root", "123456");
            PreparedStatement ps = conn.prepareStatement("SELECT * FROM user WHERE id = ?");
            ps.setString(1, id);
            ResultSet rs = ps.executeQuery();

            if (rs.next()) {
                // 将 ResultSet 中的数据提取到 User 对象中
                User user = new User();
                user.setId(rs.getString("id"));
                user.setName(rs.getString("name"));
                user.setPassword(rs.getString("password"));
                user.setEmail(rs.getString("email"));
                user.setGender(rs.getString("gender"));
                user.setBirthday(rs.getDate("birthday"));
                user.setLans(rs.getString("lans"));

                // 将 User 对象传递给 JSP 页面
                request.setAttribute("user", user);
                request.getRequestDispatcher("editUser.jsp").forward(request, response);
            } else {
                // 如果未找到用户，跳转到用户列表页面
                response.sendRedirect("userList");
            }
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("userList"); // 发生异常时跳转到用户列表页面
        }
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 设置字符编码
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String gender = request.getParameter("gender");
        String birthday = request.getParameter("birthday");
        String lans = request.getParameter("lans");

        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/FinalTest", "root", "123456");
            PreparedStatement ps = conn.prepareStatement("UPDATE user SET name = ?, password = ?, email = ?, gender = ?, birthday = ?, lans = ? WHERE id = ?");
            ps.setString(1, name);
            ps.setString(2, password);
            ps.setString(3, email);
            ps.setString(4, gender);
            ps.setDate(5, Date.valueOf(birthday));
            ps.setString(6, lans);
            ps.setString(7, id);
            ps.executeUpdate();
            response.sendRedirect("userList");
        } catch (Exception e) {
            e.printStackTrace();
            response.sendRedirect("userList"); // 发生异常时跳转到用户列表页面
        }
    }
}